﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Class_DTO;

namespace Class_DAO
{
    public class KhachHangDAO
    {

        public  DataTable getDanhSachKH()
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();            
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("getKhachHanh", cn);
            DataTable dt = new DataTable();
            ad.Fill(dt);            
            cn.Close();
            return dt;
            
        }

        public DataTable getDanhSachKH_theoten(string pTEN)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("[getKhachHang_theoten]", cn);

            SqlCommand cmd = new SqlCommand("getKhachHang_theoten", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenKH", SqlDbType.NVarChar );
            cmd.Parameters["@tenKH"].Value = pTEN;
            ad.SelectCommand  = cmd;
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;
        }

        public void ThemKhachHang_DAO(KhachHangDTO khDTO)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlCommand cmd = new SqlCommand("insertKH", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maKH", SqlDbType.Int);
            cmd.Parameters.Add("@tenKH", SqlDbType.NVarChar);
            cmd.Parameters.Add("@diaChi", SqlDbType.NVarChar);
            cmd.Parameters.Add("@soDT", SqlDbType.NVarChar);
            cmd.Parameters.Add("@soFax", SqlDbType.NVarChar);
            //cmd.Parameters.Add("@loaiKH", SqlDbType.NVarChar);
            cmd.Parameters.Add("@ghiChu", SqlDbType.NVarChar);
            cmd.Parameters["@maKH"].Direction = ParameterDirection.Output;

            
            cmd.Parameters["@tenKH"].Value = khDTO.Tenkh;
            cmd.Parameters["@diaChi"].Value = khDTO.Diachi;
            cmd.Parameters["@soDT"].Value = khDTO.Sdt;
            cmd.Parameters["@soFax"].Value = khDTO.Sfax;
            //cmd.Parameters["@loaiKH"].Value = khDTO.Loaikh ;
            cmd.Parameters["@ghiChu"].Value = khDTO.Ghichu;

            cmd.ExecuteNonQuery ();

            int mkh = Convert.ToInt32(cmd.Parameters["@maKH"].Value);
            khDTO.Makh = mkh;          
            cn.Close();
        }
        // sua lam theo mo hinh khac
        public void sua(int ma, string ten, string diachi, string sdt, string sf, string ghichu)
        {
            SqlParameter[] sp = new SqlParameter[6];
            sp[0] = new SqlParameter("@maKH", ma);
            sp[1] = new SqlParameter("@tenKH", ten);
            sp[2] = new SqlParameter("@diaChi", diachi);
            sp[3] = new SqlParameter("@soDT", sdt);
            sp[4] = new SqlParameter("@soFax", sf);
           // sp[5] = new SqlParameter("@loaiKH", loaikh);
            sp[5] = new SqlParameter("@ghiChu", ghichu);
            DataProvider.proc("updateKH", sp);
        }
        public void xoa(int s1)
        {
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@maKH", s1);
            DataProvider.proc("deleteKhachHang", sp);
        }
        
    }
}
